package com.qf.day0614.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.day0614.book.entity.Book;
import com.qf.day0614.book.mapper.BookMapper;
import com.qf.day0614.book.service.IBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ikun
 * @since 2023-06-14
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {

    public void batchSave() {
        List<Book> books = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Book book = new Book();
            book.setName("书籍" + i);
            book.setAuthor("作者" + i);
            book.setSoldnum(new Integer((int) (Math.random() * 100)));
            book.setPublish("沈阳市"+i+"区");
            books.add(book);
        }
        this.saveBatch(books);
    }

    public void batchSaves(int count, int batchSize){
        List<Book> list = new ArrayList<>(batchSize);
        Random random = new Random();
        for (int i = 0; i < count; i++) {
            Book book = new Book();
            book.setName("书籍" + i);
            book.setAuthor("作者" + i);
            book.setSoldnum(new Integer((int) (Math.random() * 100)));
            book.setPublish("沈阳市"+i+"区");
            list.add(book);
            if (i % batchSize == 0 || i == count - 1) {
                this.getBaseMapper().batchInsert(list);
                list.clear();
            }
        }
    }
}
